package com.jiukuaidao.zhulin.module.user.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.AppContext;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.api.ApiSavePwd;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.user.contract.SavePasswordContract;
import com.ta.utdid2.android.utils.StringUtils;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/27
 * Description :
 */
public class SavePasswordPresenter extends BasePresenter<SavePasswordContract.View, TasksRepository> implements SavePasswordContract.Presenter {
    public SavePasswordPresenter(TasksRepository repository, SavePasswordContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {
        loadData();
    }

    @Override
    public void loadData() {
        if (StringUtils.isEmpty(mView.getMobile()) || StringUtils.isEmpty(mView.getVeriCode())) {
            mView.toast(AppContext.getContext().getString(R.string.password_again_get_code));
            return;
        }
        if (StringUtils.isEmpty(mView.getPassword())) {
            mView.toast(AppContext.getContext().getString(R.string.password_empty));
            return;
        }
        if (StringUtils.isEmpty(mView.getTrPassword())) {
            mView.toast(AppContext.getContext().getString(R.string.password_again_empty));
            return;
        }
        if (!mView.getPassword().equals(mView.getTrPassword())) {
            mView.toast(AppContext.getContext().getString(R.string.password_different));
            return;
        }
        mRepository.loadData(new ApiSavePwd(mView.getMobile(), mView.getVeriCode(), mView.getPassword(), mView.getTrPassword(), new MyRetroCallBack() {
            @Override
            public void onSuccess(Object result, String errorCode, String msg) {
                mView.toast(AppContext.getContext().getString(R.string.new_password_save_success));
                mView.setJump();
            }

            @Override
            public void onFail(Object result, String errorCode, String msg) {
                mView.toast(msg);
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                mView.toast(resCode + "");
            }
        }));
    }
}
